/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.message;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageFactory {

	/* Tick is the most occurring message, followed by BuyCreep. Therefore
	 * those two are put at the top.
	 */
	public static enum NetworkMessage {

		Tick(MessageTick.MessagePattern, new MessageTick()),
		BuyCreep(MessageBuyCreep.MessagePattern, new MessageBuyCreep()),
		ChannelCreate(MessageChannelCreate.MessagePattern, new MessageChannelCreate()),
		ChannelJoin(MessageChannelJoin.MessagePattern, new MessageChannelJoin()),
		ChannelList(MessageChannelList.MessagePattern, new MessageChannelList()),
		Error(MessageError.MessagePattern, new MessageError()),
		Fetch(MessageFetch.MessagePattern, new MessageFetch()),
		GameEnd(MessageGameEnd.MessagePattern, new MessageGameEnd()),
		KickPlayer(MessageKickPlayer.MessagePattern, new MessageKickPlayer()),
		LoadMap(MessageLoadMap.MessagePattern, new MessageLoadMap()),
		Map(MessageMap.MessagePattern, new MessageMap()),
		MapList(MessageMapList.MessagePattern, new MessageMapList()),
		MapListOld(MessageMapListOld.MessagePattern, new MessageMapListOld()),
		Ping(MessagePing.MessagePattern, new MessagePing()),
		PlayerAuth(MessagePlayerAuth.MessagePattern, new MessagePlayerAuth()),
		PlayerForfeit(MessagePlayerForfeit.MessagePattern, new MessagePlayerForfeit()),
		PlayerInChannel(MessagePlayerInChannel.MessagePattern, new MessagePlayerInChannel()),
		PlayerInfo(MessagePlayerInfo.MessagePattern, new MessagePlayerInfo()),
		PlayerList(MessagePlayerList.MessagePattern, new MessagePlayerList()),
		Pong(MessagePong.MessagePattern, new MessagePong()),
		ReplayBasic(MessageReplayBasic.MessagePattern, new MessageReplayBasic()),
		ReplayLogSP(MessageReplayLogSP.MessagePattern, new MessageReplayLogSP()),
		ReplayLogMP(MessageReplayLogMP.MessagePattern, new MessageReplayLogMP()),
		Say(MessageSay.MessagePattern, new MessageSay()),
		SetMode(MessageSetMode.MessagePattern, new MessageSetMode()),
		Settings(MessageSettings.MessagePattern, new MessageSettings()),
		TowerBuy(MessageTowerBuy.MessagePattern, new MessageTowerBuy()),
		TowerSell(MessageTowerSell.MessagePattern, new MessageTowerSell()),
		TowerUpgrade(MessageTowerUpgrade.MessagePattern, new MessageTowerUpgrade()),
		Unknown(MessageUnknown.MessagePattern, new MessageUnknown());
		private Pattern pattern;
		private Message message;

		NetworkMessage(Pattern pattern, Message message) {
			this.pattern = pattern;
			this.message = message;
		}

		public boolean matches(String message) {
			Pattern P = this.pattern;
			Matcher m = P.matcher(message);
			Boolean matches = m.matches();
			return matches;
		}

		public Message instantiate(String message) {
			return this.message.initFromString(message);
		}
	}

	public static Message GetMessage(String message) {
		for (NetworkMessage clientMessage : NetworkMessage.values()) {
			if (clientMessage.matches(message)) {
				return clientMessage.instantiate(message);
			}
		}
		return null;
	}
}
